package Star_Wars;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class GamesPanel extends JPanel implements ActionListener ,KeyListener{
    int length;
    String fx;
    int[]snakeX=new int[600];
    int[]snakeY=new int[500];
    boolean isStart=false ;//是否开始游戏
    boolean isFail=false;
    Timer timer=new Timer(100,this);
    int foodx;
    int foody;
    int score;
    public GamesPanel() {
        init();
        this.setFocusable(true);
        this.addKeyListener(this);//添加键盘监听
        this.requestFocusInWindow();
        timer.start();
//        setKeyListener();

    }
//    public void setKeyListener(){
//        addKeyListener(new KeyListener() {
//
//
//            @Override
//            public void keyTyped(KeyEvent e) {
//                System.out.println(1);
//            }
//
//            @Override
//            public void keyPressed(KeyEvent e) {
//                System.out.println(1);
//                int keyCode = e.getKeyCode();
//                if(keyCode==KeyEvent.VK_SPACE){
//                    System.out.println(1);
//                    isStart=!isStart;
//
//                }repaint();
//                if(keyCode==KeyEvent.VK_LEFT){
//                    fx="L";
//                }if(keyCode==KeyEvent.VK_RIGHT){
//                    fx="R";
//                }if(keyCode==KeyEvent.VK_UP){
//                    fx="U";
//                }if(keyCode==KeyEvent.VK_DOWN){
//                    fx="D";
//                }
//
//            }
//
//            @Override
//            public void keyReleased(KeyEvent e) {
//                System.out.println(1);
//            }
//
//
//        });
//    }
    public void init(){//初始化数据
        length=3;
        snakeX[0]=100;
        snakeY[0]=100;
        snakeX[1]=75;
        snakeY[1]=100;
        snakeX[2]=50;
        snakeY[2]=100;
        fx="R";
        foodx=food.sfood(30);
        foody=food.sfood(24);
        score=0;
    }

    @Override
    protected void paintComponent(Graphics g) {//画板
        super.paintComponent(g);
        this.setBackground(Color.black);
        g.fillRect(0,0,900,700);
        if(!isStart){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,30));
            g.drawString("按下空格键开始游戏",250,300);
        }if(isFail){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,30));
            g.drawString("游戏失败，按下空格键继续游戏",250,300);
        }
        g.setColor(Color.red);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度:"+length,750,35);
        g.drawString("分数:"+score,750,50);
        if(fx.equals("R")){
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        if(fx.equals("L")){
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        if(fx.equals("U")){
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        if(fx.equals("D")){
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }

        for (int i = 1; i <length ; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }Data.food.paintIcon(this,g,foodx,foody);



    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode==KeyEvent.VK_SPACE){
            if(isFail){
                isFail=false;
                init();
            }else{
                isStart=!isStart;
            }

            repaint();

        }
        if(keyCode==KeyEvent.VK_LEFT&&!fx.equals("R")){
            fx="L";
        }if(keyCode==KeyEvent.VK_RIGHT&&!fx.equals("L")){
            fx="R";
        }if(keyCode==KeyEvent.VK_UP&&!fx.equals("D")){
            fx="U";
        }if(keyCode==KeyEvent.VK_DOWN&&!fx.equals("U")){
            fx="D";
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }


    @Override
    public void actionPerformed(ActionEvent e) {//定时器
        if(isStart==true&&isFail!=true){
            //边界判断
            for (int i = length-1; i >0; i--) {
                snakeX[i]=snakeX[i-1];
                snakeY[i]=snakeY[i-1];

            } if(fx.equals("R")){
                snakeX[0]=snakeX[0]+25;
                if(snakeX[0]==850){
                    isFail=true;
                   // snakeX[0]=0;
                }
            }if(fx.equals("L")){
                snakeX[0]=snakeX[0]-25;
                if(snakeX[0]==0){
                    isFail=true;
                    //snakeX[0]=850;
                }
            }if(fx.equals("U")){
                snakeY[0]=snakeY[0]-25;
                if(snakeY[0]==0){
                    isFail=true;
                    //snakeY[0]=650;
                }
            }if(fx.equals("D")){
                snakeY[0]=snakeY[0]+25;
                if(snakeY[0]==650){
                    isFail=true;
                    //snakeY[0]=0;
                }
            }

            if(foodx==snakeX[0]&&foody==snakeY[0]){
                length++;
                score++;
                while(true){
                    foodx=food.sfood(30);
                    foody=food.sfood(20);
                    if(panduan(foodx,foody,length)){
                        break;
                    }
                }

            }
            for (int i = 1; i <length ; i++) {
                if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                    isFail=true;
                }
            }

            repaint();
        }
        timer.start();
    }
    public boolean panduan(int foodx,int foody,int length){//食物不在蛇身上
        for (int i = 0; i < length; i++) {
            if(snakeX[i]==foodx&&snakeY[i]==foody){

                return false;
            }

        }
        return true;
    }
}
